<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-05-07 12:16
 *
 * 项目：upload  -  $  - install.php
 *
 * 作者：liwei 
 */

//安装或更新完成自动删除此文件和data目录

namespace modules\levpays\migrations;

!defined('INLEV') && exit('Access Denied LEV');


use Lev;
use lev\base\Adminv;
use lev\base\Migrationv;
use lev\helpers\cacheFileHelpers;
use lev\helpers\dbHelper;
use lev\helpers\ModulesHelper;
use lev\helpers\SettingsHelper;
use lev\widgets\inputs\inputsWidget;

Adminv::checkAccess();

class _update extends _migrationHelper
{

    public static function actionUpdate() {
        static::updateTableSchema();

        static::updateModuleInfo();
        static::updateSettings();

        static::insertNewData();

        inputsWidget::setCaches();
        ModulesHelper::setCaches();

        static::createGate();

        static::deleteInstallFile();
    }

    public static function updateTableSchema() {
        //_install::createTables();

        $sql = "ALTER TABLE `{{%levpays_trades}}` ADD `ip` varchar(255) NOT NULL DEFAULT '' COMMENT 'IP' AFTER `paysource`; ";
        dbHelper::existsField('{{%levpays_trades}}', 'ip') || dbHelper::executeSql($sql);

        $sql = "ALTER TABLE `{{%lev_users}}` ADD `alipayuserid` varchar(255) NOT NULL COMMENT '支付宝开放ID' AFTER `wxopenid`; ";
        dbHelper::existsField('{{%lev_users}}', 'alipayuserid') || dbHelper::executeSql($sql);

        $sql = "ALTER TABLE `{{%lev_users}}` ADD `baiduopenid` varchar(255) NOT NULL COMMENT '百度小程序开放ID' AFTER `wxopenid`; ";
        dbHelper::existsField('{{%lev_users}}', 'baiduopenid') || dbHelper::executeSql($sql);

        $sql = "ALTER TABLE `{{%levpays_trades}}` CHANGE `goodsid` `goodsid` VARCHAR(255) NOT NULL DEFAULT '0' COMMENT '模块标识对应的商品ID';";
        dbHelper::existsField('{{%levpays_trades}}', 'goodsid') && dbHelper::executeSql($sql);
    }

    public static function createGate() {

        if (!empty(Lev::$app['isDiscuz']) && is_file($__file = __DIR__ . '/data/wxpayNotifyGate_discuz.php')) {
            @file_put_contents(Lev::$aliases['@webroot'] . '/levpays_wx_notify.php', str_ireplace('!defined(', '//!defined(', @file_get_contents($__file)));
        }elseif (is_file($__file = __DIR__ . '/data/wxpayNotifyGate.php')) {
            @file_put_contents(Lev::$aliases['@webroot'] . '/levpays_wx_notify.php', str_ireplace('!defined(', '//!defined(', @file_get_contents($__file)));

        }

    }

}